﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RzCorportativo.Views
{
    public partial class Indicacoes : System.Web.UI.UserControl
    {
        private List<ButtonItem<Lib.Indicacao>> Buttons = new List<ButtonItem<Lib.Indicacao>>();
        private List<Lib.Indicacao> ListaIndicacoes { get; set; }
        public bool HasUserName { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
                return;
            if (this.ListaIndicacoes == null)
                return;
            if (!this.HasUserName)
                this.gridVagas.Rows[0].Cells[0].Visible = false;
            foreach (var indicacao in this.ListaIndicacoes)
            {
                System.Web.UI.HtmlControls.HtmlTableRow row = new System.Web.UI.HtmlControls.HtmlTableRow();
                if (this.HasUserName)
                    row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                    {
                        InnerText = indicacao.UsuarioIndicado.Nome
                    });
                row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                {
                    InnerText = indicacao.Vaga.Usuario.Nome
                });
                row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                {
                    InnerText = indicacao.Vaga.Cargo.Nome
                });
                row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                {
                    InnerText = (indicacao.Vaga.SalarioOferecido ?? 0).ToString("C")
                });
                string status = string.Empty;
                switch (indicacao.Status)
                {
                    case 1:
                        status = "AGUARDANDO AVALIAÇÃO";
                        break;
                    case 2:
                        status = "APROVADA PELO CANDIDATO";
                        break;
                    case 3:
                        status = "REPROVADA PELO CANDIDATO";
                        break;
                    case 4:
                        status = "CANCELADA";
                        break;
                    default:
                        break;
                }
                row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                {
                    InnerText = status
                });
                var str = string.Concat(this.Buttons.Select(s => s.ToString(indicacao)).ToArray());
                row.Cells.Add(new System.Web.UI.HtmlControls.HtmlTableCell()
                {
                    InnerHtml = string.Format("<div class='btn-group' data-toggle='buttons-radio'>{0}</div>", str)
                });
                this.gridVagas.Rows.Add(row);
            }
        }

        internal void Load(List<Lib.Indicacao> indicacoes)
        {
            this.ListaIndicacoes = indicacoes;
        }

        internal void AddButton(ButtonItem<Lib.Indicacao> item)
        {
            this.Buttons.Add(item);
        }
    }
}